home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3858 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: amaryllisp1.appsig.com!user
  2. From: larry_kearney@appsig.com (Lawrence Kearney)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP!! BAMBOOZLED BEGINNER!!
  5. Date: Wed, 31 Jan 1996 07:16:04 -0700
  6. Organization: Applied Signal Technology
  7. Message-ID: <larry_kearney-3101960716040001@amaryllisp1.appsig.com>
  8. References: <Pine.OSF.3.91l.960130235948.20497A-100000@saul3.u.washington.edu>
  9. NNTP-Posting-Host: amaryllisp1.appsig.com
  10.  
  11. > Hello everybody!
  12. > I'm a beginning C-programmer and for the past couple of days, I've been
  13. > totally stuck in my program. The purpose of my program is to ask the user
  14. > for an integer 'i' and raise it by a power 'n'. I've been succesful in
  15. > computing 5 to the 6th power, 3 to the 4th power, and other small-sized
  16. > equations. However, when I try to raise 5 to the 7th power, I get "12589" 
  17. > when it really should be "78125". My teacher told me that instead of using
  18. > plain 'int', I should use 'long int'. I tried that, but it's still not
  19. > working. If anyone can help me out here, I'd GREATLY appreciate it! I've
  20. > included my code below... (by the way, I'm not allowed to use the <math.h>
  21. > library)
  22. > <example code deleted>
  23.  
  24. Two problems immediately come to mind.
  25.  
  26. Number 1:   You have i_total defined as an 'int'. On many CPUs (yours
  27. included, it seems), an int is 16 bits long. This means it can hold a
  28. number that ranges from -32768 to 32767. The result of 5 to the 7th is
  29. obviously larger than this. To correct this problem, define i_total as a
  30. long or as your teacher suggested, a long int. They're the same thing.
  31.  
  32. Number 2:   In your printf statement, you use %d to format and print the
  33. result. This is fine if you are printing out an int. If you are trying to
  34. print a long, it causes problems by displaying only the lower 16 bits of
  35. the value. To fix this, change it to %ld. This will tell printf that the
  36. value to be printed is a long it so that it knows to fetch this type of
  37. integer off the calling stack.
  38.  
  39. -- 
  40. Larry Kearney                   |   "You want fries with that?"
  41. Applied Signal Technology       |
  42. larry_kearney@appsig.com        |
  43.